package com.ling.pl.security.dao;

import com.ling.pl.security.model.DefaultUrl;
import com.ling.pl.security.model.RoleResource;
import com.ling.pl.security.model.UrlComponent;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author bo.wang
 */
@Component(DefaultUrlDao.BEAN_ID)
public class DefaultUrlDao extends BaseDefaultUrlDao {
    public List<DefaultUrl> loadUrlsByRoleId(String roleId) {
        StringBuffer query = new StringBuffer();
        Map values = new HashMap<String, Object>();
        query.append("SELECT U FROM " + RoleResource.class.getName() + " R, " + DefaultUrl.class.getName() + " U where R.urlId=U.id and R.roleId=:roleId");
        values.put("roleId", roleId);
        return findBy(query, values);
    }

    public List<UrlComponent> loadComponentUrlsByRoleId(String roleId) {
        StringBuffer query = new StringBuffer();
        Map values = new HashMap<String, Object>();
        query.append("select UC FROM " + UrlComponent.class.getName() + " UC  where UC.roleId=:roleId");
        values.put("roleId", roleId);
        return findBy(query, values);
    }

    public List<DefaultUrl> loadAllUrls() {
        StringBuffer query = new StringBuffer();
        Map values = new HashMap<String, Object>();
        query.append(" from DefaultUrl ");
        query.append(" ORDER BY order ASC ");
        return findBy(query, values);
    }

    public List<DefaultUrl> loadUrlsByParentId(String parentId) {
        StringBuffer query = new StringBuffer();
        Map values = new HashMap<String, Object>();
        query.append("SELECT U FROM " + DefaultUrl.class.getName() + " U WHERE U.forNavigation=:forNavigation ");
        values.put("forNavigation", true);
        if (StringUtils.isNotEmpty(parentId)) {
            query.append("AND U.parentId = :parentId ORDER BY U.order ASC ");
            values.put("parentId", parentId);
        } else {
            query.append("AND U.parentId IS NULL ORDER BY U.order ASC ");
        }
        return findBy(query, values);
    }

    public Integer loadChildNumByParentId(String parentId) {
        StringBuffer query = new StringBuffer();
        Map values = new HashMap<String, Object>();
        query.append("SELECT count(*) FROM " + DefaultUrl.class.getName() + " U WHERE 1=1 ");
        query.append("AND U.parentId = :parentId");
        values.put("parentId", parentId);
        return queryForInt(query.toString(), values);
    }

    public List<UrlComponent> loadUrlsByUrlIdAndroleId(String urlId, String roleId) {
        StringBuffer query = new StringBuffer();
        Map values = new HashMap<String, Object>();
        query.append("from " + UrlComponent.class.getName() + " uc where uc.urlId=:urlId and uc.roleId=:roleId");
        values.put("urlId", urlId);
        values.put("roleId", roleId);
        return findBy(query, values);
    }


    public List<RoleResource> loadRoleResourceByRoleId(String roleId) {
        StringBuffer query = new StringBuffer();
        Map values = new HashMap<String, Object>();
        query.append("from " + RoleResource.class.getName() + " rr where rr.roleId=:roleId");
        values.put("roleId", roleId);
        return findBy(query, values);
    }
}

